package org.liaohailong.victorlibrary.ui

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import org.liaohailong.library.widget.refresh.RefreshLayout
import org.liaohailong.victorlibrary.R
import org.liaohailong.victorlibrary.base.BaseActivity

/**
 * Author: liaohailong
 * Date: 2019/4/25
 * Time: 20:43
 * Description:
 **/
class RefreshActivity : BaseActivity() {
    companion object {
        const val TAG = "RefreshActivity"
        fun show(context: Context) {
            val intent = Intent(context, RefreshActivity::class.java)
            context.startActivity(intent)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_refresh)

        val recyclerView = findViewById<RecyclerView>(R.id.recycler_view)
        val linearLayoutManager = LinearLayoutManager(this)
        val adapter = Adapter()

        recyclerView.layoutManager = linearLayoutManager
        recyclerView.adapter = adapter
        makeData(5, adapter)

        val refreshLayout = findViewById<RefreshLayout>(R.id.refresh_layout)
        refreshLayout.onRefreshHandler = object : RefreshLayout.OnRefreshHandler {
            override fun handlerPullRefresh(): Boolean {
                return linearLayoutManager.findFirstCompletelyVisibleItemPosition() == 0
//                return false
            }

            override fun onRefreshBegin() {
                Log.i(TAG, "refresh begin")
                refreshLayout.postDelayed({
                    makeData(5, adapter)
                    refreshLayout.stopRefresh()
                }, 3000L)
            }

            override fun onRefreshProgress(progress: Float) {
                Log.i(TAG, "refresh progress = $progress")
            }

            override fun onRefreshEnd() {
                Log.i(TAG, "refresh end")
            }
        }
    }

    private fun makeData(num: Int, adapter: Adapter) {
        val data: MutableList<String> = mutableListOf()
        for (i in 0 until num) {
            data.add(i.toString())
        }
        adapter.addData(data)
    }

    class Adapter : RecyclerView.Adapter<ViewHolder>() {
        private val data: MutableList<String> = mutableListOf()

        fun addData(dataList: List<String>) {
            val size = data.size
            data.addAll(dataList)
            notifyItemRangeChanged(size, dataList.size)
        }

        override fun getItemCount(): Int {
            return data.size
        }

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
            val view = LayoutInflater.from(parent.context).inflate(R.layout.layout_textview, parent, false)
            return ViewHolder(view)
        }

        override fun onBindViewHolder(holder: ViewHolder, position: Int) {
            val textView = holder.itemView as TextView
            textView.text = position.toString()
        }
    }

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
}